package com.jx.lcs.common.baidu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.jx.lcs.util.common.GetProp;

/**
 * 百度推送工具类（安卓-消息） 
 */
public class BaiduAndroidPushMsgUtil {
	
	private static Logger logger = LoggerFactory.getLogger(BaiduAndroidPushMsgUtil.class);

	private static final String BAIDU_API_KEY = GetProp.getConfig("BAIDU_API_KEY");
	
	private static final String BAIDU_SECRET_KEY = GetProp.getConfig("BAIDU_SECRET_KEY");
	
	//测试
	public static void main(String[] args) {
    	
		String user_id = "959609620796650102"; 
		
    	Long channel_id = 3656003654006639105L;

        String content = "55";
        
        pushMessage(user_id, channel_id, content);
    }
	
	//推送通知
	public static void pushMessage(String user_id, Long channel_id, String content) {
		/*
         * @brief 推送单播消息(消息类型为透传，由开发方应用自己来解析消息内容) message_type = 0 (默认为0)
         */

        // 1. 设置developer平台的ApiKey/SecretKey
        ChannelKeyPair pair = new ChannelKeyPair(BAIDU_API_KEY, BAIDU_SECRET_KEY);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
//                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            // 手机端的ChannelId， 手机端的UserId， 先用1111111111111代替，用户需替换为自己的
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
                                      // 4:ios 5:wp
            request.setChannelId(channel_id);
            request.setUserId(user_id);

            request.setMessage(content);

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);

            // 6. 认证推送成功
//            logger.info("--消息推送完成，" + response.getSuccessAmount());

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
            
            logger.error("--消息推送客户端错误异常", e);
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            
            logger.error("--消息推送服务端错误异常，" + String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
	}
}
